<template>
  <!-- 放置弹层组件 -->
  <el-dialog
    title="提示"
    :visible="showDialog"
    width="30%"
    @close="closeDialog"
    @open="openDiaolog"
  >
    <el-form ref="addForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择">
          <el-option
            v-for="item in employeesList"
            :key="item.value"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer />
    <span class="dialog-footer">
      <el-button @click="showDialog = false">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments } from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean
    },
    curId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      employeesList: []
    }
  },
  methods: {
    closeDialog() {
      this.$emit('close-dialog')
      // 初始化数据、清除校验结果
      this.$refs.addForm.resetFields()
    },
    async hGetEmployeeList() {
      const res = await getEmployeeSimple()
      this.employeesList = res.data
    },
    openDiaolog() {
      this.hGetEmployeeList()
    },
    // 提交表单中接口调用
    confirm() {
      // 兜底校验
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          // 调用新增接口
          addDepartments({ ...this.form, pid: this.curId })
          // 通知父组件更新列表
          this.$emit('update-list')
          // 关闭弹窗
          this.closeDialog()
        }
      })
    }
  }
}

</script>

<style>

</style>
